#include "main.h"
#include "device.h"
#include "w5500.h"
#include "cmsis_os.h"  // For osDelay
#include "usr_protocol.h"
#include "modbus_tcp.h"

uint8 buffer[2096];/*����һ��2KB�Ļ���*/
uint8 modbus_send[2048];/*����һ��2KB�Ļ���*/
uint8 mac[6]={0x00,0x08,0xdc,0x11,0x11,0x11};/*����Mac����*/
uint8 lip[4]={192,168,1,99};/*����lp����*/
uint16 MYPORT[2]={4001, 4002};  // 两个服务端口
uint8 sub[4]={255,255,255,0};/*����subnet����*/
uint8 gw[4]={192,168,1,1};/*����gateway����*/
uint8 ip[4];

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the spi_thread thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void W5500Task(void const * argument)
{

  int SocketChanel = 0;
  uint32_t len = 0;
  uint16_t modbus_len = 0;
  int i =0;

  Reset_W5500();/*Ӳ����W5500*/

  setSHAR(mac);/*����Mac��ַ*/
  setSUBR(sub);/*������������*/
  setGAR(gw);/*����Ĭ������*/
  setSIPR(lip);/*����Ip��ַ*/
	
	//Init. TX & RX Memory size of w5500
  sysinit(txsize, rxsize); /*��ʼ��8��socket*/
	
 setRTR(2000);/*�������ʱ��ֵ*/
  setRCR(5);/*����������·��ʹ���*/
	
	getSIPR (ip);
//  printf("IP : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]);
  getSUBR(ip);
 // printf("SN : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]);
  getGAR(ip);
 // printf("GW : %d.%d.%d.%d\r\n", ip[0],ip[1],ip[2],ip[3]);
 // printf("Network is ready.\r\n");
  // 初始化两个Socket通道

  for(i = 0; i < 2; i++) {
    socket(i, Sn_MR_TCP, MYPORT[i], Sn_MR_ND);
    setkeepalive(i);  // 为两个Socket都开启keepalive
  }
  
  /* USER CODE BEGIN StartTask03 */
  /* Infinite loop */
  for(;;)
  {
    // 处理两个Socket通道(0和1)
    for(i = 0; i < 2; i++)
    {
        switch(getSn_SR(i))/*��ȡsocket0��״̬*/
        {
            case SOCK_INIT:/*socket��ʼ�����*/
                listen(i);/*��TCPģʽ���������������������*/

                break;
            case SOCK_ESTABLISHED:/*socket���ӽ���*/
                
                if(getSn_IR(i) & Sn_IR_CON)
                {
                setSn_IR(i, Sn_IR_CON);/*Sn_IR�ĵ�0λ��1*/
                }

                len = getSn_RX_RSR(i);  // 获取已接收数据的大小
                if(len > 0)
                {
                  memset(buffer, 0x00, len+1);
                  recv(i, buffer, len);  // W5500从网络接收数据
                  modbus_len = tcp_modslv(buffer,modbus_send,i);
                  // 发送响应
                  send(i, modbus_send, modbus_len);
                  
                  // 等待发送完成
                  uint16_t timeout = 0;
                  while(!(getSn_IR(i) & Sn_IR_SEND_OK) && timeout < 10)
                  {
                    timeout++;
                    osDelay(1);
                  }
                  
                  // 清除发送完成标志
                  if(getSn_IR(i) & Sn_IR_SEND_OK)
                    setSn_IR(i, Sn_IR_SEND_OK);
                }


                send_ka(i);          /*��ֹ���߶Ͽ�*/
                            
                break;
            case SOCK_CLOSE_WAIT:/*socket�ȴ��ر�״̬*/
                        disconnect(i);   //��ֹ��λ������ �Ͽ��������ӵ�ʱ���ܴ�SOCKET
                break;
            case SOCK_CLOSED:/*socket�ر�*/
                socket(i,Sn_MR_TCP,MYPORT[i],Sn_MR_ND);/*��socket0��һ���˿�*/
                break;
        }
    }
    osDelay(1);
  }
  /* USER CODE END StartTask03 */
}
